<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      @click.native="hAdd('')"
                    >添加一级部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
      </el-card>
      <el-card>
        <el-tree
          :data="data"
          node-key="id"
          :props="defaultProps"
          default-expand-all
          :expand-on-click-node="false"
        >
          <template slot-scope="{ node }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ node.label }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col @click.native="fn(node)">{{
                    node.data.manager
                  }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="hAdd(node.data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hEdit(node.data.id)"
                        >编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hDel(node.data.id)"
                        >删除</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog
      :visible.sync="showDialog"
      width="60%"
      :title="isEdit ? '编辑部门' : '添加部门'"
      @close="hClose"
    >
      <deptsDialogVue
        ref="dialogRef"
        :valid-list="validList"
        :is-edit="isEdit"
        :pid="curId"
        @close="Hclose"
      />
    </el-dialog>
  </div>
</template>

<script>
import { getTree } from '@/utils/getTree'
import { getDepartments, delDepartmentAPI } from '@/api/departments'
import deptsDialogVue from './deptsDialog.vue'
export default {
  components: {
    deptsDialogVue
  },
  // 依赖一份树形数据
  data() {
    return {
      data: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      showDialog: false,
      curId: '',
      isEdit: false, // 控制是否是编辑状态
      // 子组件表单验证所需数据
      validList: []
    }
  },
  created() {
    this.initDepartments()
  },
  methods: {
    // 删除处理函数
    async doDel() {
      try {
        const res = await delDepartmentAPI(this.curId)
        console.log('', res)
        // 重发请求刷新数据
        this.initDepartments()
      } catch (err) {
        console.log('', err)
      }
    },
    // 点击删除按钮
    hDel(id) {
      this.$confirm('确定删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.curId = id
          this.doDel()
        })
        .catch(() => {})
    },
    // 点击编辑按钮
    hEdit(id) {
      // 显示弹框
      this.showDialog = true
      // 编辑状态
      this.isEdit = true
      // 更新curId
      this.curId = id
      this.$nextTick(() => {
        this.$refs.dialogRef.reqDepartment()
      })
    },
    // 子组件成功添加 | 编辑 向父组件抛出事件
    Hclose() {
      this.showDialog = false
      this.initDepartments()
    },
    // 关闭dialog触发
    hClose() {
      this.$nextTick(() => {
        this.$refs.dialogRef.resetForm()
      })
    },
    // 点击添加按钮
    hAdd(id) {
      this.showDialog = true
      this.curId = id
    },
    // test
    fn(obj) {
      // console.log(obj)
    },
    // 初始化部门列表
    async initDepartments() {
      try {
        const res = await getDepartments()
        // console.log('', res)
        res.data.depts.shift()
        // 保存数据
        this.data = getTree(res.data.depts)
        this.validList = res.data.depts.map((ele) => {
          return {
            id: ele.id,
            name: ele.name,
            code: ele.code,
            pid: ele.pid
          }
        })
      } catch (err) {
        console.log('', err)
      }
    }
  }
}
</script>
